package com.xinnet.mms.empcustomrelation.service.impl;

import java.sql.SQLException;
import java.util.List;
import java.util.UUID;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.xinnet.mms.empcustomrelation.dao.EmpCustomRelationDao;
import com.xinnet.mms.empcustomrelation.service.EmpCustomRelationService;
import com.xinnet.mms.entity.CustomStateHist;
import com.xinnet.mms.entity.Customer;
import com.xinnet.mms.entity.Dept;
import com.xinnet.mms.entity.Emp;
import com.xinnet.mms.entity.EmpCustomRelation;
import com.xinnet.mms.entity.Product;
import com.xinnet.mms.entity.PushHistory;
import com.xinnet.mms.utils.CustomerManage;

@Service("empCustomRelationService")
public class EmpCustomRelationServiceImpl implements EmpCustomRelationService{
	
	@Autowired
	private EmpCustomRelationDao empCustomRelationDao;
	
	/**
	 * 插入人员客户关系历史
	 */
	@Override
	public void addEmpCustomRelation(EmpCustomRelation empCustomRelation) throws SQLException {
		empCustomRelationDao.addEmpCustomRelation(empCustomRelation);
	}
	
	/**
	 * 封装人员客户关系信息
	 */
	@Override
	public EmpCustomRelation packageEmpCustomRelation(Emp emp, Customer customer, Product product, Dept dept, PushHistory pushHistory) {
		EmpCustomRelation empCustomRelation=new EmpCustomRelation();
		empCustomRelation.setId(UUID.randomUUID().toString());
		empCustomRelation.setCustomId(customer.getCusId());
		empCustomRelation.setEmpId(emp.getEmpno());
		empCustomRelation.setCustomType(customer.getCustomerType());
		empCustomRelation.setCustomState(CustomerManage.ACCEPT_NO);
		empCustomRelation.setProductId(product.getId());
		empCustomRelation.setProductCode(product.getProductCode());
		empCustomRelation.setPushHistId(pushHistory.getId());
		empCustomRelation.setPusherId(customer.getPusherId());
		empCustomRelation.setPushDesc(customer.getRemark());
		empCustomRelation.setDeptno(dept.getId());
		empCustomRelation.setCompanyId(customer.getCompanyId());
		return empCustomRelation;
	}
	/**
	 * 获取所有流转的客户
	 * @return List
	 */
	public List<EmpCustomRelation> getTransferCustom(){
		
		List<EmpCustomRelation> empCustomRelationList = null;//empCustomRelationDao.getTransferCustom();
		return empCustomRelationList;
		
	}
	
	/**
	 * 删除EmpCustomRelation
	 * @param customId
	 * @param empNo
	 */
	public void delEmpCustomRelationByCustomIdAndEmpNo(String customId,String empNo,String productCode){
		
		empCustomRelationDao.delEmpCustomRelationByCustomIdAndEmpNo(customId, empNo,productCode);
		
	}

	/**
	 * 用销售ID和用户ID查询是否接收
	 */
	@Override
	public Integer getAcceptCustomerCountByCusIdAndEmpno(String cusId, String empno,String productCode) throws SQLException {
		Customer customer=new Customer();
		customer.setCusId(cusId);
		customer.setEmpno(empno);
		customer.setProductCode(productCode);
		return empCustomRelationDao.getAcceptCustomerCountByCusIdAndEmpno(customer);
	}

	@Override
	public void updateEmpCustomRelation(EmpCustomRelation empCustomRelation) {
		
		empCustomRelationDao.updateEmpCustomRelation(empCustomRelation);
	}

	/**
	 * 处理调配修改人员客户关系
	 */
	@Override
	public void updateEmpCustomRelationByMix(String empno,String cusId) throws SQLException {
		Customer customer=new Customer();
		customer.setEmpno(empno);
		customer.setCusId(cusId);
		empCustomRelationDao.updateEmpCustomRelationByMix(customer);
	}

	@Override
	public EmpCustomRelation getEmpCustomRelationById(String id) {
		
		return empCustomRelationDao.getEmpCustomRelationById(id);
	}
	
	/**
	 * 通过主键IDStr获取客户销售关系记录
	 * @param id
	 * @return
	 */
	public List<EmpCustomRelation> getEmpCustomRelationByIdStr(String idStr){
		return empCustomRelationDao.getEmpCustomRelationByIdStr(idStr);
		
	}

	@Override
	public void updateEmpCustomRelationById(EmpCustomRelation empCustomRelation) {
		
		empCustomRelationDao.updateEmpCustomRelationById(empCustomRelation);
	}

	/**
	 * 根据客户关系ID 删除客户关系表记录
	 */
	@Override
	public void delByEcrId(String ecrId) throws SQLException {
		empCustomRelationDao.delByEcrId(ecrId);
	}
	/**
	 * 根据客户ID获取 员工客户关系
	 * @param customId
	 * @return
	 */
	public List<EmpCustomRelation>  getEmpCustomRelationByCustomId(String customId){
		List<EmpCustomRelation> empCustomRelationList = empCustomRelationDao.getEmpCustomRelationByCustomId(customId);
		return empCustomRelationList;
		
	}

	/**
	 * 修改客户关系表客户ID
	 */
	@Override
	public void updateEmpCustomRelationCusId(Customer customer) throws SQLException {
		empCustomRelationDao.updateEmpCustomRelationCusId(customer);
	}

	@Override
	public EmpCustomRelation getEmpCustomRelationByCondition(
			EmpCustomRelation empCustomRelation) {
		
		return empCustomRelationDao.getEmpCustomRelationByCondition(empCustomRelation);
	}

	/**
	 * 用员工id和产品code查询 客户关系表记录
	 */
	@Override
	public List<EmpCustomRelation> getEmpCustomRelationByCusIdAndProductCode (String cusId, String productCode) {
		Customer customer=new Customer();
		customer.setCusId(cusId);
		customer.setProductCode(productCode);
		List<EmpCustomRelation> list=empCustomRelationDao.getEmpCustomRelationByCusIdAndProductCode(customer);
		if(list==null || list.isEmpty()) {
			return null;
		}
		else {
			return list;
		}
	}
	
	/**
	 * 用员工id和产品code查询 客户关系表记录
	 */
	@Override
	public List<EmpCustomRelation> getEmpCustomRelationByCusId(String cusId) {
		Customer customer=new Customer();
		customer.setCusId(cusId);
		List<EmpCustomRelation> list=empCustomRelationDao.getEmpCustomRelationByCusId(customer);
		if(list==null || list.isEmpty()) {
			return null;
		}
		else {
			return list;
		}
	}
	
	public void updateServiceDate(EmpCustomRelation empCustomRelation){
		empCustomRelationDao.updateServiceDate(empCustomRelation);
	}

	/**
	 * 关系表ID修改关系表
	 */
	@Override
	public void updateEmpCustomRelationByEcrId(EmpCustomRelation empCustomRelation) {
		empCustomRelationDao.updateEmpCustomRelationByEcrId(empCustomRelation);
	}

	@Override
	public List<EmpCustomRelation> getEmpCustomRelationListByCondition(
			EmpCustomRelation empCustomRelation) {
		return empCustomRelationDao.getEmpCustomRelationListByCondition(empCustomRelation);
	}

	/**
	 * 根据推送历史ID获取关系表记录
	 */
	@Override
	public EmpCustomRelation getEcrByPushHistIdAndPusherId(CustomStateHist csh) {
		return empCustomRelationDao.getEcrByPushHistIdAndPusherId(csh);
	}

	/**
	 * 根据客户ID删除关系表中的记录
	 */
	@Override
	public void delEmpCustomRelationByCusId(String cusId) {
		empCustomRelationDao.delEmpCustomRelationByCusId(cusId);
	}

	/**
	 * 用员工id和产品code查询 和客户类型 客户关系表记录
	 */
	@Override
	public List<EmpCustomRelation> getEmpCustomRelationByCusIdAndProductCodeAndCustomType(String cusId, String productCode,Integer customerType) {
		Customer customer=new Customer();
		customer.setCusId(cusId);
		customer.setProductCode(productCode);
		customer.setCustomType(customerType);
		List<EmpCustomRelation> list=empCustomRelationDao.getEmpCustomRelationByCusIdAndProductCodeAndCustomType(customer);
		if(list==null || list.isEmpty()) {
			return null;
		}
		else {
			return list;
		}
	}

	@Override
	public int getTransferCustomCount(Customer customer) {
		return empCustomRelationDao.getTransferCustomCount(customer);
	}
	
	public List<EmpCustomRelation> getTransferCustom(Customer customer){
		return empCustomRelationDao.getTransferCustom(customer);
	}
}
